unit main;

{$IFDEF FPC}
  {$mode delphi}
{$ENDIF}

//{$DEFINE DEBUG}

interface

procedure mainProc();

implementation

uses
  zglHeader, zglChipmunk, g_vars, cp_space, cp_shapes, res_manager, ui_main;

var
  LMB_DOWN: Boolean = False;

procedure Init();
begin
  cpInitChipmunk();
  InitSpace();
  InitBorders();
  RuleBall();
  //AddBar();
  resMan := TResManager.Create;
  resMan.ResDir := FOLDER_RESOURCES;
  EyeBall := resMan.LoadTex('eye_ball.png');
  ui_main.Init();
end;

procedure Draw();
begin
  batch2d_Begin();
//    cpDrawSpace(Space, False);
    cp_shapes.Draw();

    ui_main.Draw();
  batch2d_End();
end;

procedure Update(dt: Double);
var
  sh: PcpCircleShape;
  pt: cpVect;
  b: PcpBody;
begin
  if key_Press(K_ESCAPE) then
    zgl_Exit();

  mBall.p := cpv(mouse_X, mouse_Y);

  if key_Press(K_F) then
    DrawFace := True;
  if key_Press(K_D) then
    DrawFace := False;

  if (mouse_Down(M_BLEFT)) and (not LMB_DOWN) then
  begin
    LMB_DOWN := True;
    PinUp();
  end;
  if (mouse_Up(M_BLEFT)) and (LMB_DOWN) then
  begin
    LMB_DOWN := False;
    PinDown();
  end;

  case RightState of
    rtAdd: begin
      if mouse_Click(M_BRIGHT) then
        AddBall(mouse_X(), mouse_Y());
    end;
    rtBlow: begin
      if mouse_Down(M_BRIGHT) then
      begin
        pt := cpv(mouse_X, mouse_Y);
        sh := PcpCircleShape(cpSpacePointQueryFirst(Space, pt, 2, 0));
        if Assigned(sh) then
        begin
          b := sh.shape.body;
          sh.r := sh.r + 0.04 * dt;
          cpBodySetMass(b, sh.r / 10);
        end;
      end;
    end;
    rtDel: begin
      if mouse_Down(M_BRIGHT) then
      begin
        pt := cpv(mouse_X, mouse_Y);
        sh := PcpCircleShape(cpSpacePointQueryFirst(Space, pt, 2, 0));
        if Assigned(sh) then
        begin
          DelBall(sh.shape.body);
          cpSpaceRemoveBody(Space, sh.shape.body);
          cpSpaceRemoveShape(Space, @sh.shape);
        end;
      end;
    end;
  end;


  key_ClearState();
  mouse_ClearState();
  //спейс
  cpSpaceStep(Space, dt / 1000);

  ui_main.Update(dt);
end;

procedure Exit();
begin
  resMan.Free;
end;

procedure mainProc();
begin
  Randomize();

  //иннициализация игрового движка
  zglLoad(libZenGL);
  cpLoad(libChipmunk);

//{$IFNDEF DEBUG}
  zgl_Disable(APP_USE_LOG);
//{$ENDIF}
  zgl_Enable( APP_USE_UTF8 );
//  zgl_Enable(APP_USE_AUTOPAUSE);
//  zgl_Disable( COLOR_BUFFER_CLEAR );

  DirHome := u_CopyStr(PChar(zgl_Get(DIRECTORY_HOME)));
  DirApp  := u_CopyStr(PChar(zgl_Get(DIRECTORY_APPLICATION)));
  DirRes  := DirApp + FOLDER_RESOURCES;

  zgl_Enable(APP_USE_UTF8);
  wnd_ShowCursor(True);

  zgl_Reg(SYS_LOAD,   @Init);
  zgl_Reg(SYS_DRAW,   @Draw);
  zgl_Reg(SYS_UPDATE, @Update);
  zgl_Reg(SYS_EXIT,   @Exit);

  wnd_SetCaption(APPLICATION_NAME);

{$IFNDEF DEBUG}
  SCREEN_WIDTH := zgl_Get(DESKTOP_WIDTH);
  SCREEN_HEIGHT := zgl_Get(DESKTOP_HEIGHT);
{$ELSE}
  SCREEN_WIDTH := 800;
  SCREEN_HEIGHT := 600;
  SCREEN_FULL := False;
{$ENDIF}
  Scr_SetOptions(SCREEN_WIDTH, SCREEN_HEIGHT, REFRESH_DEFAULT, SCREEN_FULL, True);

  zgl_Init();

  cpFree();
  zglFree();
end;

end.

